package com.jy.adscm.operation.aftersales.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.aftersales.bean.ShCustomerInfo;
import com.jy.adscm.operation.aftersales.bean.ShMachine;
import com.jy.adscm.operation.aftersales.bean.ShSpaceParts;
import com.jy.adscm.operation.aftersales.bean.ShSpacePartsDetail;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShCustomerInfoMapper;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShMachineMapper;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShSpacePartsDetailMapper;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShSpacePartsMapper;
import com.jy.adscm.operation.aftersales.service.ShSpacePartsDetailService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-08-19
 */
@Service
public class ShSpacePartsDetailServiceImpl extends ServiceImpl<ShSpacePartsDetailMapper, ShSpacePartsDetail> implements ShSpacePartsDetailService {
     @Autowired
    ShCustomerInfoMapper shCustomerInfoMapper;
     @Autowired
    ShMachineMapper shMachineMapper;
     @Autowired
    JyUserMapper jyUserMapper;
     @Autowired
    ShSpacePartsMapper shSpacePartsMapper;
    @Override
    public boolean insertShSpacePartsDetail(ShSpacePartsDetail shSpacePartsDetail) {
        //查询售后
        ShSpaceParts shSpaceParts = shSpacePartsMapper.selectById(shSpacePartsDetail.getPid());
        shSpaceParts.setState(1);
         shSpacePartsMapper.updateById(shSpaceParts);
        boolean insert = super.insert(shSpacePartsDetail);
        return insert;
    }

    @Override
    public List<ShSpacePartsDetail> selectByPid(Integer pid) {
        Wrapper<ShSpacePartsDetail> wrapper=new EntityWrapper<>();
        wrapper.eq("pid",pid);
        List<ShSpacePartsDetail> shSpacePartsDetails = super.selectList(wrapper);
        for (ShSpacePartsDetail shSpacePartsDetail : shSpacePartsDetails) {
         if (AdscmBaseUtils.getInstance().isNotEmpty(shSpacePartsDetail.getCustomer(),null)){
             ShCustomerInfo shCustomerInfo = shCustomerInfoMapper.selectById(shSpacePartsDetail.getCustomer());
             shSpacePartsDetail.setCustomerName(shCustomerInfo);
         }
         if (AdscmBaseUtils.getInstance().isNotEmpty(shSpacePartsDetail.getMachine(),null)){
             ShMachine shMachine = shMachineMapper.selectById(shSpacePartsDetail.getMachine());
             shSpacePartsDetail.setMachineName(shMachine);
         }
         if (AdscmBaseUtils.getInstance().isNotEmpty(shSpacePartsDetail.getCreator(),null)){
             JyUser jyUser = jyUserMapper.selectById(shSpacePartsDetail.getCreator());
             shSpacePartsDetail.setCreatorName(jyUser.getName());
         }

        }
        return shSpacePartsDetails;
    }
}
